var errorSet = require("./.errorSet/errorSet.json");
var fs = require("./fs");

function JsonError(errorMsg) {
	if (!(this instanceof JsonError)) {
		return new JsonError(errorMsg);
	}
	errorMsg.message && (errorMsg = errorMsg.message);
	if (errorSet.hasOwnProperty(errorMsg)) {
		this.errorCode = errorSet[errorMsg];
	} else {
		//错误不存在，创建新的错误号，重写errorSet.json文件
		this.errorCode = errorSet[errorMsg] = ((Object.keys(errorSet).length + 1) / 100000).toString().substr(2);
		fs.fs.writeFileSync("server/_include/.errorSet/errorSet.json", JSON.stringify(errorSet));
	}
	// var _e = new Error(errorMsg);
	// console.log(_e.stack);
	this.errorMsg = errorMsg;
	this.errorTime = +new Date;
};
JsonError.prototype = {
	toString: function() {
		try {
			var result = JSON.stringify(this);
		} catch (e) {
			result = JSON.stringify(String(e));
		}
		return result;
	}
}
module.exports = JsonError;